﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DDF.Data;
using DDF.Data.Providers;
using System.Data;
using ARR.Entities.Gateways;

namespace ARR.DataModel.Repositories
{
    public static partial class HistoryDetailsRepository
    {
        public static void GenerateHistory(int rangeReviewId)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Timeout = 300;

                database.NewCommand("History_GenerateHistoryDataFromCurrentRangeReview", CommandType.StoredProcedure);
                database.AddInParameter("@RangeReviewId", DbType.Int32, rangeReviewId);

                database.ExecuteNonQuery();
            }
        }
        
        public static List<HistoryDetailRow> SelectHistory(int rangeReviewId)
        {
            List<HistoryDetailRow> historyDetailList = new List<HistoryDetailRow>();

            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Timeout = 300;

                database.NewCommand("History_SelectFromCurrentRangeReview", CommandType.StoredProcedure);
                database.AddInParameter("@RangeReviewId", DbType.Int32, rangeReviewId);
                IDataReader reader = database.ExecuteReader();

                LoadFromSP(reader, historyDetailList);
            }

            return historyDetailList;
        }

        public static void ClearOldHistory(int rangeReviewId)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Timeout = 300;

                database.NewCommand("History_ClearOldData", CommandType.StoredProcedure);
                database.AddInParameter("@RangeReviewId", DbType.Int32, rangeReviewId);
                database.ExecuteNonQuery();
            }
        }

        

        private static void LoadFromSP(IDataReader reader, List<HistoryDetailRow> HistoryDetailList)
        {
            HistoryDetailRow row;

            while (reader.Read())
            {
                row = new HistoryDetailRow()
                {
					CategoryId = Util.CastDbValue<int?>(reader["CategoryId"]),
					Category = Util.CastDbValue<string>(reader["Category"]),
					Universe = Util.CastDbValue<string>(reader["Universe"]),
					Role = Util.CastDbValue<string>(reader["Role"]),
					AverageVolume = Util.CastDbValue<object>(reader["AverageVolume"]),
					AverageSales = Util.CastDbValue<decimal?>(reader["AverageSales"]),
					AverageGIM = Util.CastDbValue<decimal?>(reader["AverageGIM"]),
					AverageGIMP = Util.CastDbValue<decimal?>(reader["AverageGIMP"]),
					AverageDelistedVolume = Util.CastDbValue<object>(reader["AverageDelistedVolume"]),
					AverageDelistedSales = Util.CastDbValue<decimal?>(reader["AverageDelistedSales"]),
					AverageDelistedGIM = Util.CastDbValue<decimal?>(reader["AverageDelistedGIM"]),
					SS_VolumeCY = Util.CastDbValue<object>(reader["SS_VolumeCY"]),
					SS_SalesCY = Util.CastDbValue<decimal?>(reader["SS_SalesCY"]),
					SS_GIMCY = Util.CastDbValue<decimal?>(reader["SS_GIMCY"]),
					SS_PIDiffCY = Util.CastDbValue<decimal?>(reader["SS_PIDiffCY"]),
					SS_ChargeOffCY = Util.CastDbValue<decimal?>(reader["SS_ChargeOffCY"]),
					SS_RebatesCY = Util.CastDbValue<decimal?>(reader["SS_RebatesCY"]),
					SS_InventoryCY = Util.CastDbValue<decimal?>(reader["SS_InventoryCY"]),
					SS_VolumePY = Util.CastDbValue<object>(reader["SS_VolumePY"]),
					SS_SalesPY = Util.CastDbValue<decimal?>(reader["SS_SalesPY"]),
					SS_GIMPY = Util.CastDbValue<decimal?>(reader["SS_GIMPY"]),
					SS_PIDiffPY = Util.CastDbValue<decimal?>(reader["SS_PIDiffPY"]),
					SS_ChargeOffPY = Util.CastDbValue<decimal?>(reader["SS_ChargeOffPY"]),
					SS_RebatesPY = Util.CastDbValue<decimal?>(reader["SS_RebatesPY"]),
					SS_InventoryPY = Util.CastDbValue<decimal?>(reader["SS_InventoryPY"]),
					Gondola = Util.CastDbValue<decimal?>(reader["Gondola"]),
					TransactionArea = Util.CastDbValue<decimal?>(reader["TransactionArea"]),
					GrabAndGo = Util.CastDbValue<decimal?>(reader["GrabAndGo"]),
					WIC = Util.CastDbValue<decimal?>(reader["WIC"]),
					OpenCooler = Util.CastDbValue<decimal?>(reader["OpenCooler"]),
					Freezer = Util.CastDbValue<decimal?>(reader["Freezer"]),
					Forecourt = Util.CastDbValue<decimal?>(reader["Forecourt"]),
					OtherExhibition = Util.CastDbValue<decimal?>(reader["OtherExhibition"]),
					SKUsWithSales = Util.CastDbValue<int?>(reader["SKUsWithSales"]),
					NewSKUs = Util.CastDbValue<int?>(reader["NewSKUs"]),
					SKUsInSalesFiles = Util.CastDbValue<int?>(reader["SKUsInSalesFiles"]),
					DelistProposal = Util.CastDbValue<int?>(reader["DelistProposal"]),
					Sales10 = Util.CastDbValue<int?>(reader["Sales10"]),
					Sales20 = Util.CastDbValue<int?>(reader["Sales20"]),
					Sales30 = Util.CastDbValue<int?>(reader["Sales30"]),
					Sales40 = Util.CastDbValue<int?>(reader["Sales40"]),
					Sales50 = Util.CastDbValue<int?>(reader["Sales50"]),
					Sales60 = Util.CastDbValue<int?>(reader["Sales60"]),
					Sales70 = Util.CastDbValue<int?>(reader["Sales70"]),
					Sales80 = Util.CastDbValue<int?>(reader["Sales80"]),
					Sales90 = Util.CastDbValue<int?>(reader["Sales90"]),
					Sales100 = Util.CastDbValue<int?>(reader["Sales100"]),
					Bottom5 = Util.CastDbValue<int?>(reader["Bottom5"]),
					HurdleRate = Util.CastDbValue<int?>(reader["HurdleRate"]),
					ActiveCategories = Util.CastDbValue<int?>(reader["ActiveCategories"]),
					CategoriesWithInvAnalysis = Util.CastDbValue<int?>(reader["CategoriesWithInvAnalysis"]),
					Country = Util.CastDbValue<string>(reader["Country"]),
					Range = Util.CastDbValue<string>(reader["Range"])
                };
                                
                HistoryDetailList.Add(row);
            }

            reader.Close();
        }
    }
}
